/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.InteractionConstraint;
import org.eclipse.uml2.uml.InteractionOperand;

/**
 * Command to move a guard from one InteractionOperand to another. 
 *
 * <p>The following attributes must be set for the command to complete
 * successfully:</p>
 * <ul>
 *   <li>Guard
 *   <li>Operand - to receive the guard
 * </ul>
 * 
 * @author frankd@ifi.uio.no
 */
public class MoveGuardCommand extends Command {
	
	private InteractionOperand operand, oldOperand;
	private InteractionConstraint guard, oldGuard;
	
	public void setGuard(final InteractionConstraint guard) {
		this.guard = guard;
	}
	
	public void setOperand(final InteractionOperand operand) {
		this.operand = operand;
	}
	
	@Override
	public void execute() {
		oldGuard = operand.getGuard();
		oldOperand = (InteractionOperand) guard.getContext();
		operand.setGuard(guard);
	}
	
	@Override
	public void undo() {
		oldOperand.setGuard(guard);
		operand.setGuard(oldGuard);
		oldOperand = null;
		oldGuard = null;
	}
	
	@Override
	public void redo() {
		oldGuard = operand.getGuard();
		oldOperand = (InteractionOperand) guard.getContext();
		operand.setGuard(guard);
	}
	
	@Override
	public void dispose() {
		guard = null;
		operand = null;
		oldGuard = null;
		oldOperand = null;
	}
}
